import { requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { useCallback, useMemo } from 'react';
import { ManageAPI } from '../common/type';
import { ManageSearchColumns, ManageTabSwitch, supplierManageTabSwitch } from '../mconfig';
import { getBaseUrl } from '@/services/urlconst';
let baseUrl = getBaseUrl();

/**
 * 采购供应商统计 - 接口
 * */
const ManageServices = (curRes) => {
  const loadMdicMapList = [];
  /**
   * 管理页获取数据方法
   * */
  const { tableProps, data, loading, run, refresh } = useRequest(
    (reqParams: ManageAPI.IAssetTableRequestType) => {
      let PostParams = {
        current: 1,
        pageSize: 10,
        sort: 'createTime', //根据时间排序-默认
        order: 'desc', //降序-默认
        ...ManageTabSwitch(curRes),
        ...reqParams,
      };

      let reqUrl = `${baseUrl}/purchaseReportController/reportSupplierByStep/${PostParams?.params}`;

      return requestFunComponFiex('POST', reqUrl, PostParams);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /**
   * 重新请求的方法
   * */
  const reloadRequest = useCallback((reqParams: ManageAPI.IAssetTableRequestType | any) => {
    run({ ...reqParams });
  }, []);

  /**
   * 页面数据及分页信息
   * */
  const tableValues = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  /**
   * 搜索功能配置columns
   * */
  const searchList = useMemo(() => {
    return {
      loadMdicMapList,
      searchList: ManageSearchColumns(loadMdicMapList),
    };
  }, [loadMdicMapList]);

  return {
    loading,
    reloadRequest,
    reloadManageData: refresh,
    ...tableValues, //页面列表值
    ...searchList,
  };
};

/**
 * 供应商管理 - 接口
 * */
const SupplierManageServices = (curRes) => {
  /**
   * 过滤供应商
   * */
  let creditList: string[] = ['12100000MB2001605R', '12110000MB1N5794X2'];

  /**
   * 接口
   * */
  const { tableProps, data, loading, run, refresh } = useRequest(
    (opt: Record<string, any>) => {
      /**
       * 获取权限参数
       * */
      const queryTypeObj: Record<string, any> = supplierManageTabSwitch(curRes);

      /**
       * 请求体
       * */
      const PostParams: Record<string, any> = {
        ...queryTypeObj,
        current: 1,
        pageSize: 10,
        sort: 'createTime',
        order: 'desc',
        creditID: creditList?.toString(),
        selectSign: false,
        ...opt,
      };

      const requestURL = `${baseUrl}/supplierInfoController/dataGrid/${PostParams?.params}`;
      return requestFunComponFiex('POST', requestURL, PostParams);
    },
    {
      manual: false,
      paginated: true,
      defaultPageSize: 99999,
    },
  );

  /**
   * 页面列表
   */
  let returnTableProps = useMemo(() => {
    return {
      supplierData: data?.rows,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  /**
   * 重写获取接口
   * */
  const getSupplierData = (reqBody) => {
    const request = { ...reqBody };
    run({ ...request });
  };

  return {
    supplierLoading: loading,
    getSupplierData,
    getSupplierRefresh: refresh,
    ...returnTableProps,
  };
};

export { ManageServices, SupplierManageServices };
